﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Data;

namespace MobileUI
{
    public partial class ShoppingCart : System.Web.UI.Page
    {
        SqlConnection conn;
        SqlCommand cmd;
        SqlDataAdapter da;
        SqlDataReader dr;

        protected void Page_Load(object sender, EventArgs e)
        {
            conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["MobileConnectionString"].ConnectionString);
            if (Session["CID"] == null)
            {
                lblMessage.Text = "You must login for using Shopping Cart";
                pCart.Visible = false;
            }
            else
            {
                if (Request.QueryString["Id"] != null)
                {
                    if (Session["Cart"] == null)
                    {
                        Session["Cart"] = Request.QueryString["Id"];
                    }
                    else
                    {
                        if (Session["Cart"].ToString().Contains(Request.QueryString["Id"]))
                        {
                            lblMessage.Text = "Product is added to Shopping Cart";
                        }
                        else
                        {
                            Session["Cart"] += "|" + Request.QueryString["Id"];
                        }
                    }
                }
                else
                {
                    if (Session["Cart"] == null)
                    {
                        lblMessage.Text = "Shopping Cart is empty";
                        pCart.Visible = false;
                    }

                }
                LoadCart();
            }
        }
        //This is function Load Data to Shopping Cart.
        #region Load Cart Data
        private void LoadCart()
        {
            if (Session["Cart"] != null)
            {
                String[] cart = Session["Cart"].ToString().Split('|');
                bool first = true;
                String sql = "SELECT TOP (16) Product.Id, Product.ProviderId, Product.Name, Product.Description, Product.Quantity, Product.Price, Product.Image, Provider.Name AS Expr1, Provider.Id AS Expr2 FROM Product INNER JOIN Provider ON Product.ProviderId = Provider.Id";
                sql += " where (Product.Id=";
                for (int i = 0; i < cart.Length; i++)
                {
                    if (first)
                    {
                        sql += cart[i] + " ) ";
                        first = false;
                    }
                    else
                    {
                        sql += " or (Product.Id = " + cart[i] + " ) ";
                    }
                }
                try
                {
                    conn.Open();
                    da = new SqlDataAdapter(sql, conn);
                    DataSet ds = new System.Data.DataSet();
                    da.Fill(ds);
                    gvCart.DataSource = ds;
                    gvCart.DataBind();

                }
                catch (Exception ex)
                {

                }
                finally
                {
                    if (conn.State != ConnectionState.Closed)
                    {
                        conn.Close();
                    }
                }
            }
        }
        #endregion
        protected void btnReset_Click(object sender, EventArgs e)
        {
            Session["Cart"] = null;
            Response.Redirect("Default.aspx");
        }

        protected void btnCheckout_Click(object sender, EventArgs e)
        {
            string sql = "insert into [Order] values(1, '"+DateTime.Now.ToString("MM/dd/yyyy")+"', "+ Session["CID"]+")";
            string[] item = Session["Cart"].ToString().Split('|');
            for (int i = 0; i < item.Count(); i++)
            {
                sql += " insert into OrderDetail values((select top (1) Id from [Order] Order by Id DESC), 1, "+item[i]+")";  
            }            
            try
            {
                conn.Open();
                cmd = new SqlCommand()
                {
                    CommandText = sql,
                    CommandType = CommandType.Text,
                    Connection = conn
                };
                cmd.ExecuteNonQuery();
                conn.Close();
                Session["Cart"] = null;
                Response.Write("<script>alert('Your order has been sent. We will contact you as soon as possible for confirm your order details. Thanks you!'); window.location = '../Default.aspx'</script>");
            }
            catch (Exception)
            {
                Response.Write("<script>alert('Some error ocurred.');</script> ");
            }            
        }
        protected void btnCalcel_Click(object sender, EventArgs e)
        {

        }
    }
}